/*
 * Copyright (C) 2018 The Asiainfo-Ability Authors
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package org.asiainfo.ability.manager.service;


import org.asiainfo.ability.gateway.cache.CacheService;
import org.asiainfo.ability.gateway.dao.AbilityDao;
import org.asiainfo.ability.gateway.dao.ServiceDao;
import org.asiainfo.ability.gateway.dao.ClientDao;
import org.asiainfo.ability.base.queue.OperEvent;
import org.asiainfo.ability.base.queue.support.CacheEvent;
import org.asiainfo.ability.manager.configuration.CacheConfig;
import org.asiainfo.ability.manager.mq.MessageQueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Service;

/**
 * @Author: visen
 * @Date: 2018/2/1
 * @Description:
 */
@Service
public class AdminService {


    @Autowired
    private ServiceDao serviceDao;

    @Autowired(required = false)
    private AbilityDao abilityDao;

    @Autowired(required = false)
    private ClientDao clientDao;

    @Autowired
    private SeqService seqService;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private MessageQueueService mqService;

    @Autowired
    private RedisCacheManager cacheManager;

    public void publishEvent(OperEvent event) {
        mqService.publishEvent(event);
    }

    public <T> T get(String cacheName, Class<T> claz, String... keys) {
        return cacheService.get(geneKey(cacheName, keys), claz);
    }


    public void refresh(String cacheName, Object value, String... keys) {
        StringBuilder sb = new StringBuilder();
        for (Object obj : keys) {
            sb.append(obj.toString());
        }
        CacheConfig.CustomRedisCache customRedisCache = (CacheConfig.CustomRedisCache)cacheManager.getCache(cacheName);
        if(customRedisCache != null) {
            customRedisCache.put(sb.toString(), value);
        }
        noticeLocalCache(cacheName, keys);
    }


    public void noticeLocalCache(String cacheName, String... keys) {
        CacheEvent event = new CacheEvent();
        event.setName(cacheName);
        StringBuilder sb = new StringBuilder();
        for (Object obj : keys) {
            sb.append(obj.toString());
        }
        event.setKey(sb.toString());
        publishEvent(event);
    }

    private String geneKey(String cacheName, String... keys) {
        StringBuilder sb = new StringBuilder(cacheName).append(":");
        for (Object obj : keys) {
            sb.append(obj.toString());
        }
        return sb.toString();
    }

    public void remove(String cacheName, String... keys) {

        cacheService.remove(geneKey(cacheName, keys));
        noticeLocalCache(cacheName, keys);
    }

}
